#include "maindialog.h"
#include "ui_maindialog.h"
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QDebug>
#include <QtWidgets>

MainDialog::MainDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MainDialog)
{
    ui->setupUi(this);

    //defintion of variables
    arduino_is_avaliable = false;
    arduino_port_name = "";
    arduino = new QSerialPort;

    //find the serialportinfo(vendor_id, product_id)
    /*
    qDebug() << "Number of the avaliable Ports: " << QSerialPortInfo::availablePorts().length();
    foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){
        qDebug() <<"Has Vendor ID : " <<serialPortInfo.hasVendorIdentifier();
        if(serialPortInfo.hasVendorIdentifier()){
            qDebug() <<"Vendor ID : " <<serialPortInfo.vendorIdentifier();
        }
        qDebug() <<"Has Product ID : " <<serialPortInfo.hasProductIdentifier();
        if(serialPortInfo.hasProductIdentifier()){
            qDebug() <<"Product ID : " <<serialPortInfo.productIdentifier();
        }
    }
    */
    //
    foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){
        if(serialPortInfo.hasVendorIdentifier() && serialPortInfo.hasProductIdentifier()){
            if(serialPortInfo.vendorIdentifier() == arduino_uno_vendor_id){
                if(serialPortInfo.productIdentifier() == arduino_uno_poduct_id){
                    arduino_port_name = serialPortInfo.portName();
                    arduino_is_avaliable = true;
                }
            }
        }
    }

     //open and configure a serialport
    if(arduino_is_avaliable){
        arduino->setPortName(arduino_port_name);
        arduino->open(QSerialPort::WriteOnly);
        arduino->setBaudRate(QSerialPort::Baud9600);
        arduino->setDataBits(QSerialPort::Data8);
        arduino->setParity(QSerialPort::NoParity);
        arduino->setStopBits(QSerialPort::OneStop);
        arduino->setFlowControl(QSerialPort::NoFlowControl);
    }
    else{
     //give error message if not avaliable
        QMessageBox::warning(this,"Port error","Couldn't find the arduino");
    }
}

MainDialog::~MainDialog()
{
    if(arduino->isOpen()){
        arduino->close();
    }
    delete ui;
}

   //connect to the ui,defination of slot functions
void MainDialog::on_red_slider_valueChanged(int value)
{
   //write the value to the arduino
   MainDialog::updateRGB(QString("r%1").arg(value));
   //change ui the value of the red_value_lable
   ui->red_value_lable->setText(QString("<span style=\" font-size:18pt; font-weight:600;\">%1</span>")
                                .arg(value));
   qDebug() << value;
}


void MainDialog::on_green_slider_valueChanged(int value)
{
    ui->green_value_lable->setText(QString("<span style=\" font-size:18pt; font-weight:600;\">%1</span>")
                                   .arg(value));
    MainDialog::updateRGB(QString("g%1").arg(value));
    qDebug() <<value;
}

void MainDialog::on_blue_slider_valueChanged(int value)
{
    ui->blue_value_lable->setText(QString("<span style=\" font-size:18pt; font-weight:600;\">%1</span>")
                                  .arg(value));
    MainDialog::updateRGB(QString("b%1").arg(value));
    qDebug() <<value;

}

//realize the fundation of writing to arduino
void MainDialog::updateRGB(QString command)
{
    if (arduino->isWritable()){
        arduino->write(command.toStdString().c_str());
    }else{
        qDebug() <<"Couldn't write to serial!";
    }
}
